package com.scau.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.scau.dao.BaseDao;
import com.scau.dao.IEmplCateDao;
import com.scau.vo.EmployeeCategory;

/**
 * @author Jinglun
 * @create 2017年4月19日
 */
public class EmplCateDaoImpl extends BaseDao implements IEmplCateDao {

	private Session session = null;
	private Transaction tx = null;
	private String hql = "";
	
	@Override
	public boolean saveEmplCate(EmployeeCategory employeeCategory) {
		session = getSession();
		tx = session.beginTransaction();
		
		hql = "from EmployeeCategory ec where ec.name ='" 
				+ employeeCategory.getName() + "'";
		EmployeeCategory exist = (EmployeeCategory) session.createQuery(hql).uniqueResult();
		
		if (exist != null) {
			tx.commit();
			session.close();
			return false;
		} else {
			session.save(employeeCategory);
			tx.commit();
			session.close();
			return true;
		}
	}

	@Override
	public boolean deleteEmplCate(int id) {
		session = getSession();
		tx = session.beginTransaction();
		EmployeeCategory ec = (EmployeeCategory) session.get(EmployeeCategory.class, id);
		session.delete(ec);
		tx.commit();
		session.close();
		return true;
	}

	@Override
	public List<EmployeeCategory> listEmplCate() {
		session = getSession();
		
		hql = "from EmployeeCategory ec";
		Query query = session.createQuery(hql);
		List<EmployeeCategory> emplCateList = query.list();
	
		session.close();
		return emplCateList;
	}

	@Override
	public boolean updateEmplCate(EmployeeCategory employeeCategory) {
		session = getSession();
		tx = session.beginTransaction();
		
		// 查询要修改的实体
		EmployeeCategory upEC = (EmployeeCategory) session.get(EmployeeCategory.class, employeeCategory.getId());
		
		upEC.setName(employeeCategory.getName());
		upEC.setRemark(employeeCategory.getRemark());
		upEC.setGmtModified(employeeCategory.getGmtModified());
		
		tx.commit();
		session.close();
		return true;
	}

}
